package com.llyb.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.llyb.tingshu.album.mapper.*;
import com.llyb.tingshu.album.service.BaseCategoryService;
import com.llyb.tingshu.common.cache.TsCache;
import com.llyb.tingshu.model.album.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {
		ArrayList<JSONObject> list = new ArrayList<>();

		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);

		Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().
				collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		for (Map.Entry<Long, List<BaseCategoryView>> listEntry : map.entrySet()) {

			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("categoryName",listEntry.getValue().get(0).getCategory1Name());
			jsonObject1.put("categoryId",listEntry.getKey());

			List<BaseCategoryView> category1ViewList = listEntry.getValue();
			// 根据getCategory2Id 分组
			Map<Long, List<BaseCategoryView>> map2 = category1ViewList.stream().
					collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));


			ArrayList<Object> list2 = new ArrayList<>();
			// 二级分类
			for (Map.Entry<Long, List<BaseCategoryView>> entry : map2.entrySet()) {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryName",entry.getValue().get(0).getCategory2Name());
				jsonObject2.put("categoryId",entry.getKey());

				List<BaseCategoryView> category2ViewList = entry.getValue();
				// 三级分类
				List<JSONObject> list3 = category2ViewList.stream().
						map(BaseCategoryView -> {
							JSONObject jsonObject3 = new JSONObject();
							jsonObject3.put("categoryName", BaseCategoryView.getCategory3Name());
							jsonObject3.put("categoryId", BaseCategoryView.getCategory3Id());
							return jsonObject3;
						}).collect(Collectors.toList());

				jsonObject2.put("categoryChild",list3);
				list2.add(jsonObject2);
			}

			jsonObject1.put("categoryChild",list2);
			list.add(jsonObject1);
		}


		return list;
	}

	@Override
	public List<BaseAttribute> getAttribute(String category1Id) {
		return baseAttributeMapper.selectAttributeValueList(category1Id);

	}

	/**
	 * 根据三级id获取分类详情(专辑详情页)
	 * @param category3Id
	 * @return
	 */
	@Override
	@TsCache(prefix = "album:category:")
	public BaseCategoryView getCategoryView(Long category3Id) {
		BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
		return baseCategoryView;
	}

	/**
	 * 获取一级分类下的所有三级分类
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {

		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
				.eq(BaseCategory2::getCategory1Id, category1Id));

		List<BaseCategory3> baseCategory3List = null;
		if (!CollectionUtils.isEmpty(baseCategory2List)) {
			// 只要2级分类id集合
			List<Long> category2IdList = baseCategory2List.stream().map(BaseCategory2::getId)
					.collect(Collectors.toList());

			baseCategory3List = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
					.in(BaseCategory3::getCategory2Id, category2IdList)
					.orderByDesc(BaseCategory3::getIsTop)
							.orderByAsc(BaseCategory3::getId)
					.last(" limit 7 ")
			);
		}
		return baseCategory3List;
	}

	@Override
	public JSONObject getBaseCategoryList(Long category1id) {

		List<JSONObject> list1 = new ArrayList<>();

		JSONObject jsonObject1 = new JSONObject();
		// 根据一级id 查询
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().
				eq(BaseCategoryView::getCategory1Id, category1id));

		if (!CollectionUtils.isEmpty(baseCategoryViews)) {
			// 赋值一级JSON 数据
			jsonObject1.put("categoryName",baseCategoryViews.get(0).getCategory1Name());
			jsonObject1.put("categoryId",baseCategoryViews.get(0).getCategory1Id());

			Map<Long, List<BaseCategoryView>> category2List = baseCategoryViews.stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

			ArrayList<JSONObject> jsonObject2List = new ArrayList<>();

			for (Map.Entry<Long, List<BaseCategoryView>> listEntry : category2List.entrySet()) {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryName",listEntry.getValue().get(0).getCategory2Name());
				jsonObject2.put("categoryId",listEntry.getKey());

				ArrayList<JSONObject> jsonObject3List = new ArrayList<>();
				listEntry.getValue().stream().forEach(category3View->{
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryName",category3View.getCategory3Name());
					jsonObject3.put("categoryId",category3View.getCategory3Id());
					jsonObject3List.add(jsonObject3);
				});

				jsonObject2.put("categoryChild",jsonObject3List);
				jsonObject2List.add(jsonObject2);
			}
			jsonObject1.put("categoryChild",jsonObject2List);
		}


		return jsonObject1;
	}

	/**
	 * 获取所有一级分类
	 * @return
	 */
	@Override
	public List<BaseCategory1> findAllCategory1() {

		return baseCategory1Mapper.selectList(null);
	}
}
